export default class JJDateUtil {

    // 数据格式化
    static formatNumber(n) {
        n = n.toString()
        return n[1] ? n : '0' + n
    }

    /**
    * 将当前时间转化为年 月 日 时 分 秒
    * format：返回格式--20180713092518
    */
    static formatCurrentTimeWithUnunit() {
        let returnArr = [];
        let date = new Date();
        returnArr.push(date.getFullYear());
        returnArr.push(JJDateUtil.formatNumber(date.getMonth() + 1));
        returnArr.push(JJDateUtil.formatNumber(date.getDate()));
        returnArr.push(JJDateUtil.formatNumber(date.getHours()));
        returnArr.push(JJDateUtil.formatNumber(date.getMinutes()));
        returnArr.push(JJDateUtil.formatNumber(date.getSeconds()));
        let format = returnArr.join('');
        return format;
    }

    // 将时间20180713092518转化为时间戳
    static dateFromString(dateString) {
        let year = parseInt(dateString.substring(0, 4));
        let month = parseInt(dateString.substring(4, 6)) - 1;
        let day = parseInt(dateString.substring(6, 8));
        let hour = parseInt(dateString.substring(8, 10));
        let min = parseInt(dateString.substring(10, 12));
        let sec = parseInt(dateString.substring(12, 14));
        return new Date(year, month, day, hour, min, sec);
    }

    // 按照指定的时间格式返回时间
    static dateFormat(dateTime, fmt) {
        var date = new Date(dateTime);
        fmt = fmt || 'yyyy-MM-dd';
        var o = {
          "M+": date.getMonth() + 1, //月份
          "d+": date.getDate(), //日
          "h+": date.getHours(), //小时
          "m+": date.getMinutes(), //分
          "s+": date.getSeconds(), //秒
          "q+": Math.floor((date.getMonth() + 3) / 3), //季度
          "S": date.getMilliseconds() //毫秒
        };
        if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o)
          if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        return fmt;
      }
}
